﻿@inject IHttpService _httpService


@inject NavigationManager NavigationManager


<div class="">
    <div class="mb-3">
        <CnGalWebSite.Components.Buttons.MasaButton Text="添加" IsAsync="true" OnClick="AddAward" Icon="mdi-plus" Color="success" />
        <CnGalWebSite.Components.Buttons.MasaButton Text="导入" IsAsync="true" OnClick="BatchItems" Icon="mdi-database-import-outline " Color="info" Class="ms-3" />

    </div>

    @foreach (var item in Awards.OrderByDescending(s=>s.Priority))
    {
        <div class="mb-3">
            <div class="card p-3 w-100" style="background-color:transparent">
                <div style="display: flex; justify-content: space-between;">
                    <div class="mt-2">
                        @if (string.IsNullOrWhiteSpace(item.Image) == false)
                        {
                            <img src="@item.Image" alt="@item.Name" style="max-width:200px;" class="mb-3" />
                        }

                        <p class="text-truncate-1">
                            名称： @item.Name
                        </p>
                        <p class="text-truncate-1">
                            数量： @item.Count
                        </p>
                        <p class="text-truncate-1">
                            赞助商： @(item.Sponsor??"无")
                        </p>
                        <p class="text-truncate-1">
                            链接： @(item.Link ?? "无")
                        </p>
                        <p class="text-truncate-1">
                            类型： @item.Type.GetDisplayName()
                        </p>
                        <p class="text-truncate-1">
                            附加积分： @item.Integral
                        </p>
                        <p class="text-truncate-1 mb-1">
                            优先级： @item.Priority
                        </p>
                    </div>
                    <div class="ms-3" style=" display: flex; flex-direction: column; ">
                        <CnGalWebSite.Components.Buttons.MasaButton Fab IsAsync="true" OnClick="@(()=> DeleteAward(item))" Icon="mdi-trash-can-outline" Color="error" />
                        <CnGalWebSite.Components.Buttons.MasaButton Fab IsAsync="true" OnClick="@(()=> EditAward(item))" Icon="mdi-pencil" Color="success" Class="mt-3"/>
                    </div>
                </div>
                @if (item.Type == LotteryAwardType.ActivationCode)
                {
                    <div>
                        <EditPrizeListCard Prizes="item.Prizes" />
                    </div>
                }

            </div>
        </div>
    }

</div>
<EditAwardCard @ref="editAwardCard" OnRefreshTable="OnRefreshTable"/>

<MDialog @bind-Value="_batchDialog"
         MaxWidth="800">
    <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Cards.Lotteries.Prizes.BatchCard OnBatched="OnBatched" @ref="batchCard" Title="奖品" />
</MDialog>

@code {
    [Parameter]
    public List<EditLotteryAwardModel> Awards { get; set; }
    EditAwardCard editAwardCard;

    CnGalWebSite.Shared.MasaComponent.Shared.Editors.Cards.Lotteries.Prizes.BatchCard batchCard;
    private bool _batchDialog;


    public async Task DeleteAward(EditLotteryAwardModel item)
    {
        Awards.Remove(item);
        StateHasChanged();
    }
    public async Task EditAward(EditLotteryAwardModel item)
    {
        await editAwardCard.Refresh(true, item);
    }

    public void OnRefreshTable(EditLotteryAwardModel model)
    {
        if(Awards.Any(s=>s==model)==false)
        {
            Awards.Add(model);
        }
        StateHasChanged();

        
    }
    public async Task AddAward()
    {
        await editAwardCard.Refresh(true, new EditLotteryAwardModel());
    }

    public void BatchItems()
    {
        _batchDialog = true;
        StateHasChanged();
    }


    public void OnBatched(List<string> model)
    {
        //检查是否符合要求
        foreach (var item in model)
        {
            Awards.Add(new EditLotteryAwardModel
                {
                    Name = item,
                    Count=1
                });

        }
        _batchDialog = false;
        StateHasChanged();
    }
}
